import com.mysql.jdbc.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * DriverManager 进行 JDBC 的查询
 */
public class SelectCity2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.选择mysql驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获得连接
        Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java33?user=root&password=12345678&characterEncoding=UTF-8&useSSL=true");

        //3.获得执行器
        PreparedStatement statement=connection.prepareStatement("select * from city");
        //4.执行sql
        ResultSet resultSet=statement.executeQuery();
        while (resultSet.next()){
            City city=new City();
            city.setId(resultSet.getInt("id"));
            city.setName(resultSet.getString("name"));
            System.out.println(city);
        }
        //5.关闭资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
